﻿using DotNetExtensions.Core.SQLite.Attributes;
using DotNetExtensions.Core.SQLite.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DotNetExtensions.Core.SQLite
{
    public class Property
    {
        public string Name { get; set; }
        public string OriginName { get; set; }
        public bool Key { get; set; }
        public bool IsJson { get; set; }

        public bool Ignore { get; set; }
        public string ColumnType { get; set; } = "Text";
        public Type Type { get; set; }

        public bool IsMap { get; set; } = true;
        public bool JoinAble { get; set; }

        public Property()
        {

        }

        public Property(PropertyInfo propertyInfo)
        {
            var attribute = propertyInfo.GetCustomAttribute<SQLiteColumnAttribute>();
            if (attribute == null)
            {
                Name = propertyInfo.Name;
                OriginName = propertyInfo.Name;
                Type = propertyInfo.PropertyType;
                ColumnType = ParseSQLiteColumnType(propertyInfo.PropertyType, SQLiteType.Auto);
            }
            else
            {
                OriginName = propertyInfo.Name;
                Ignore = attribute.Ignore;
                Name = attribute.Name ?? propertyInfo.Name;
                Type = propertyInfo.PropertyType;
                Key = attribute.Key;
                ColumnType = ParseSQLiteColumnType(propertyInfo.PropertyType, attribute.ColumnType);
                IsJson = attribute.IsJson;
            }
        }

        private string ParseSQLiteColumnType(Type propertyType, SQLiteType type)
        {
            if (type == SQLiteType.Auto)
            {
                type = SQLiteType.Text;

                if (propertyType == typeof(int)) type = SQLiteType.Integer;
                if (propertyType == typeof(short)) type = SQLiteType.Integer;
                if (propertyType == typeof(uint)) type = SQLiteType.Integer;
                if (propertyType == typeof(ushort)) type = SQLiteType.Integer;
                if (propertyType == typeof(UInt16)) type = SQLiteType.Integer;
                if (propertyType == typeof(UInt32)) type = SQLiteType.Integer;
                if (propertyType == typeof(Int16)) type = SQLiteType.Integer;
                if (propertyType == typeof(Int32)) type = SQLiteType.Integer;
                if (propertyType == typeof(long)) type = SQLiteType.Integer;
                if (propertyType == typeof(Int64)) type = SQLiteType.Integer;

                if (propertyType == typeof(float)) type = SQLiteType.Real;
                if (propertyType == typeof(double)) type = SQLiteType.Real;
                if (propertyType == typeof(decimal)) type = SQLiteType.Real;

                if (propertyType == typeof(string)) type = SQLiteType.Text;
                if (propertyType == typeof(String)) type = SQLiteType.Text;

                if (propertyType == typeof(byte[])) type = SQLiteType.Blob;
                if (propertyType == typeof(DBNull)) type = SQLiteType.Null;
            }

            return Enum.GetName(typeof(SQLiteType), type);

        }
    }
}
